package com.ziroom.finance.rabbitmq.common.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
import org.zefer.pd4ml.PD4PageMark;

import freemarker.template.TemplateException;

/**
 * HTML转pdf文件
 *
 * @author lcj_up
 */
public class HtmltoPDF {

    private static final Logger logger = LoggerFactory.getLogger(HtmltoPDF.class);

//		public static String pdfHeaderLogo = "http://pic.ziroom.com.cn/oa/images/logo.png";

    public static PD4ML pd4ml = null;
    protected static int userSpaceWidth = 780;

    private static void initPD4ML() {
        pd4ml = new PD4ML();

        pd4ml.setPageInsets(new java.awt.Insets(10, 30, 10, 30));
        pd4ml.setHtmlWidth(720);
        pd4ml.setPageSize(PD4Constants.A4);

        try {
            pd4ml.useTTF("java:fonts", true);
        } catch (FileNotFoundException e) {
            logger.error("---HtmltoPDF-加载字体失败", e);
        }
        pd4ml.setDefaultTTFs("SimHei", "SimSun", "YouYuan");

        //页眉
        PD4PageMark headerMark = new PD4PageMark();
        headerMark.setAreaHeight(10);
        headerMark.setTitleAlignment(PD4PageMark.RIGHT_ALIGN);
        headerMark.setHtmlTemplate("");
        pd4ml.setPageHeader(headerMark);
        //页脚
        PD4PageMark footerMark = new PD4PageMark();
        footerMark.setAreaHeight(20);
        footerMark.setInitialPageNumber(1);
        headerMark.setPagesToSkip(0);
        footerMark.setTitleAlignment(PD4PageMark.CENTER_ALIGN);
        footerMark.setHtmlTemplate("<div align='center' style='font-size: 10px'>$[page]/$[total]</div>");
        pd4ml.setPageFooter(footerMark);

    }

    /**
     * htmlstr装pdf文件
     *
     * @param htmlStr
     * @param filePath
     * @throws Exception
     */
    public static void htmlStrToPdf(String htmlStr, String filePath) throws IOException {
        if (!new File(filePath).getParentFile().exists()) {
            new File(filePath).getParentFile().mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(filePath);

        if (pd4ml == null) {
            initPD4ML();
        }

        pd4ml.enableDebugInfo();
        pd4ml.render(new StringReader(htmlStr), fos);
    }

    /**
     * 生成pdf文件字节
     *
     * @param htmlStr html content
     * @throws IOException
     */
    public static byte[] html2PdfByte(String htmlStr) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (pd4ml == null) {
            initPD4ML();
        }

        pd4ml.enableDebugInfo();
        pd4ml.render(new StringReader(htmlStr), out);
        return out.toByteArray();
    }

//		public static void setPdfHeaderLogo(String pdfHeaderLogo) {
//			HtmltoPDF.pdfHeaderLogo = pdfHeaderLogo;
//		}

    public static void main(String[] args) throws IOException, TemplateException {
        Map<String, Object> m = new HashMap<String, Object>();
        String html = HtmlGenerator.generate("template/loan.html", m);
        try {
            htmlStrToPdf(html, "d:/tmp/templete.pdf");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
